package com.seejoke.mybatis.generator.plugins;

import java.util.List;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;

/**
 * lombok 注解插件
 *
 * @author yangzhongying
 * @date 2019/7/30 16:42
 */
public class LombokPlugin extends PluginAdapter {
  private FullyQualifiedJavaType dataAnnotation;

  public LombokPlugin() {
    dataAnnotation = new FullyQualifiedJavaType("lombok.Data");
  }

  @Override
  public boolean validate(List<String> warnings) {
    return true;
  }

  /**
   * 拦截 普通字段
   *
   * @param topLevelClass topLevelClass
   * @param introspectedTable introspectedTable
   * @return boolean
   */
  @Override
  public boolean modelBaseRecordClassGenerated(
      TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
    addDataAnnotation(topLevelClass);
    return true;
  }

  /**
   * 拦截 主键
   *
   * @param topLevelClass topLevelClass
   * @param introspectedTable introspectedTable
   * @return boolean
   */
  @Override
  public boolean modelPrimaryKeyClassGenerated(
      TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
    addDataAnnotation(topLevelClass);
    return true;
  }

  /**
   * 拦截 blob 类型字段
   *
   * @param topLevelClass topLevelClass
   * @param introspectedTable introspectedTable
   * @return boolean
   */
  @Override
  public boolean modelRecordWithBLOBsClassGenerated(
      TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
    addDataAnnotation(topLevelClass);
    return true;
  }

  /**
   * Prevents all getters from being generated. See SimpleModelGenerator
   *
   * @param method m
   * @param topLevelClass topLevelClass
   * @param introspectedColumn introspectedColumn
   * @param introspectedTable introspectedTable
   * @param modelClassType modelClassType
   * @return boolean
   */
  @Override
  public boolean modelGetterMethodGenerated(
      Method method,
      TopLevelClass topLevelClass,
      IntrospectedColumn introspectedColumn,
      IntrospectedTable introspectedTable,
      ModelClassType modelClassType) {
    return false;
  }

  /**
   * Prevents all setters from being generated See SimpleModelGenerator
   *
   * @param method method
   * @param topLevelClass topLevelClass
   * @param introspectedColumn introspectedColumn
   * @param introspectedTable introspectedTable
   * @param modelClassType modelClassType
   * @return boolean
   */
  @Override
  public boolean modelSetterMethodGenerated(
      Method method,
      TopLevelClass topLevelClass,
      IntrospectedColumn introspectedColumn,
      IntrospectedTable introspectedTable,
      ModelClassType modelClassType) {
    return false;
  }

  /**
   * addDataAnnotation
   * @param topLevelClass topLevelClass
   */
  protected void addDataAnnotation(TopLevelClass topLevelClass) {
    topLevelClass.addImportedType(dataAnnotation);
    topLevelClass.addAnnotation("@Data");
    //topLevelClass.addAnnotation("@NoArgsConstructor");
  }
}
